package TestConcurrent;

import java.util.concurrent.CountDownLatch;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张杰
 * Date: 2022-04-18
 * Time: 12:01
 */
public class DemoCountDownLatch {
    public static void main(String[] args) throws InterruptedException {
        //比赛的初始赛道
        CountDownLatch countDownLatch = new CountDownLatch(10);
        for (int i = 0; i < 10; i++) {
            Thread t = new Thread(() -> {
                try {
                    Thread.sleep(3000);
                    System.out.println(Thread.currentThread().getName()+"到达终点!");
                    countDownLatch.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            t.start();
        }
        //裁判等待所有线程到达
        //当这些线程没有执行完,await就一直等待
        countDownLatch.await();
        System.out.println("比赛结束!");
    }
}
